// pages/minePage/stateInformateMainte/stateInformateMainte.js
import { lgRequest } from '../../../utils/ajax'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: getApp().globalData.statusBarHeight,
    titleBarHeight: getApp().globalData.titleBarHeight,
    sysInfo:getApp().globalData.sysInfo,
    isIphoneX:getApp().globalData.isIphoneX,
    rightIcon:'https://www.mingchuliangzao.com.cn:8632/profile/upload/2022/07/15/392d59b0c51fadfabf6131d5930507b3.png',
    delectImg:'https://www.mingchuliangzao.com.cn:8632/profile/upload/2022/07/19/713a49f43240fae4d44090d92d7f9cf6.png',
    parentList:[],
    powList:[],//电力分组
    netList:[],//网络分组
    userInfoList:[],//负责人列表
    childrenList:[],
    showSelectDevice:false,
    selectType:'',
    file:{}
  },
  //获取一级分组列表
  fList() {
    let params = {}
    lgRequest('fList', params, {
      errAlert: false,
      doSuccess: r => {
        if (r.code == 200) {
          this.setData({
            parentList : r.data.list,
            powList:r.data.powList,
            netList:r.data.netList
          })
        } else {
          wx.showToast({
            title: r.msg,
            icon: 'none'
          })
        }
      },
      doComplete: r => {
      }
    })
  },
  //获取负责人列表
  userList(){
    let params = {}
    lgRequest('userList', params, {
      errAlert: false,
      doSuccess: r => {
        if (r.code == 200) {
          console.log(r.data,'这是负责人数据');
          this.setData({
            userInfoList:r.data
          })
        } else {
          wx.showToast({
            title: r.msg,
            icon: 'none'
          })
        }
      },
      doComplete: r => {
      }
    })
  },
  //获取二级分组列表
  getChildList(){
    let params = {
      fatherId:this.data.file.packetId
    }
    lgRequest('packetList', params, {
      errAlert: false,
      doSuccess: r => {
        if (r.code == 200) {
          this.setData({
            childrenList : r.data
          })
        } else {
          wx.showToast({
            title: r.msg,
            icon: 'none'
          })
        }
      },
      doComplete: r => {
      }
    })
  },
  //选择
  selectItem(e){
    let item = e.currentTarget.dataset.item
    switch(this.data.selectType){
      case '1':
        this.setData({
          ['file.packetId']:item.id,
          ['file.oneName']:item.name
        })
        break;
      case '2':
        this.setData({
          ['file.packetId']:item.id,
          ['file.packetName']:item.name
        })
        break;
      case '3': //电力分组
        this.setData({
          ['file.power']:item.id,
          ['file.powerName']:item.name
        })
        break;
      case '4': //网络分组
        this.setData({
          ['file.network']:item.id,
          ['file.networkName']:item.name
        })
        break;
      case '5': //负责人
        this.setData({
          ['file.principal']:item.userId,
          ['file.nickName']:item.nickName
        })
        break;
    }
    this.setData({
      showSelectDevice:false
    })
  },
  //获取当前定位
  getLoaction(){
    let that = this
    wx.getLocation({
      type: 'gcj02',
      success(res) {
          console.log(res,'@@');
        const latitude = res.latitude
        const longitude = res.longitude
        var str = res.latitude + "," + res.longitude;
        that.parseAddress(str)
        that.setData({
          ['file.gps']:`${longitude},${latitude}`
        })
      }
    })
  },
  parseAddress(str, address) {
    let that = this
    wx.request({
      url: 'https://apis.map.qq.com/ws/geocoder/v1/',
      data: {
        "key": "FCGBZ-2J36P-VGWDM-V2XYT-QZRCV-6SBB2",
        "location": str
      },
      method: 'GET',
      success: function (r) {
          console.log(r,'@@@');
        that.setData({
          ['file.address']:r.data.result.address
        })
      },
      complete() {}
    });
  },
  changeInput(e){
   
    let nameStr = e.currentTarget.dataset.namestr
    // console.log(e.currentTarget.dataset.nameStr)
    console.log(e.currentTarget.dataset.namestr)
    this.setData({
      [nameStr]:e.detail.value,
    })
  },
  goBack(){
    let pages = getCurrentPages();//当前页面    （pages就是获取的当前页面的JS里面所有pages的信息）
    let prevPage = pages[pages.length - 2];//上一页面（prevPage 就是获取的上一个页面的JS里面所有pages的信息）
    prevPage.setData({
      zhanIdInfo:this.data.file,
    }) 
        wx.navigateBack({
          delta: 1
        })
  },
  showPop(e){
    let type = e.currentTarget.dataset.type
    if(type==2){
      if(this.data.selectType!=1){
        wx.showToast({
          title: '请选择一级分组',
          icon:'none'
        })
        return 
      } else {
        this.getChildList()
      }
    }
    this.setData({
      selectType:type,
      showSelectDevice:true
    })
  },
  hideSelectPop(){
    this.setData({
      showSelectDevice:false
    })
  },
  changeStatus(){
    let file = this.data.file
    let params = {
      gps:file.gps,
      name : file.name,
      packetId :file.packetId,
      principal  :file.principal ,
      zid  :file.zid,
      power  :file.power?file.power:this.data.zhanIdInfo.power,
      network   :file.network ?file.network :this.data.zhanIdInfo.network,
      remark  :file.remark?file.remark:'',
      
    }
    let zhanIdInfo = this.data.zhanIdInfo
    zhanIdInfo.gps=file.gps,
    zhanIdInfo.name = file.name,
    zhanIdInfo.packetId =file.packetId,
    zhanIdInfo.principal  =file.principal ,
    zhanIdInfo.zid  =file.zid,
    zhanIdInfo.power  =file.power?file.power:this.data.zhanIdInfo.power,
    zhanIdInfo.network   =file.network ?file.network :this.data.zhanIdInfo.network,
    zhanIdInfo.remark  =file.remark?file.remark:'',
    console.log(zhanIdInfo)
    this.setData({
      zhanIdInfo
    })
   
    lgRequest("changeStatus",params,{
      doSuccess:r=>{
        console.log(r,'修改成功没')
        this.goBack()
        // let item = encodeURIComponent(JSON.stringify(this.data.zhanIdInfo));
        // wx.navigateTo({
        //   url: `../stateDetail/stateDetail?zhanIdInfo=${item}`,
        // })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let item =JSON.parse(decodeURIComponent(options.item));
    // let file={
    //   packetName:item.oneName,
    //   childName:item.packetName,
    //   name:item.name,
    //   address:item.address,
    //   powerName:item.powerName,
    //   networkName:item.networkName,
    //   principalName:item.nickName,
    //   remark:item.remark,
    //   gps:item.gps,
    //   zid:item.zid,
    //   principal:item.principal,
    //   packetId :item.packetId,

    // }
    let file = item
    console.log(options)
    console.log(file,'这是file')
    this.fList()
    this.userList()
    this.setData({
      zhanIdInfo: JSON.parse(decodeURIComponent(options.item)),
      file
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})